#include "message.h"

Message::Message()
{
    m_timestamp = QDateTime::currentDateTime();
}

Message::Message(const QString &username, const QString &content)
    : m_username(username), m_content(content)
{
    m_timestamp = QDateTime::currentDateTime();
}

QString Message::username() const
{
    return m_username;
}

QString Message::content() const
{
    return m_content;
}

QDateTime Message::timestamp() const
{
    return m_timestamp;
}

QString Message::toString() const
{
    // 消息格式：时间戳|用户名|内容
    return QString("%1|%2|%3")
            .arg(m_timestamp.toString("yyyyMMddHHmmss"))
            .arg(m_username)
            .arg(m_content);
}

Message Message::fromString(const QString &str)
{
    QStringList parts = str.split("|");
    if (parts.size() >= 3) {
        Message msg;
        msg.m_timestamp = QDateTime::fromString(parts[0], "yyyyMMddHHmmss");
        msg.m_username = parts[1];
        msg.m_content = parts[2];
        return msg;
    }
    return Message();
}